﻿using System;
using System.Net;
using System.Windows.Forms;
using DVDFilmoteka.Settings;

namespace DVDFilmoteka.Forms
{
    public partial class Settings : Form
    {
        public Settings()
        {
            InitializeComponent();
        }

        private void Settings_Load(object sender, EventArgs e)
        {
            SettingsManager manager = SettingsManager.Instance;

            checkBoxAutomaticVersionCheck.Checked = manager.AutomaticUpdate;
            checkBoxViewResults.Checked = manager.ViewResults;
            checkBoxUseProxy.Checked = manager.UseProxy;
            checkBoxDontChangeGenres.Checked = manager.DontChangeGenres;
            checkBoxDontChangeNames.Checked = manager.DontChangeNames;
            checkBoxLoadPicturesOnLine.Checked = manager.LoadPicturesOnline;
            checkBoxBeta.Checked = manager.BetaInform;
            if (manager.UseProxy)
            {
                checkBoxUseDefaultProxy.Checked = manager.UseDefaultProxy;
                if (!manager.UseDefaultProxy)
                {
                    textBox1.Text = manager.ProxyIPAddress.ToString();
                    textBox2.Text = manager.ProxyPort.ToString();
                } else
                {
                    textBox1.Text = "";
                    textBox2.Text = "";
                }
            } else
            {
                textBox1.Text = "";
                textBox2.Text = "";
                checkBoxUseDefaultProxy.Checked = true;
            }

            checkBox2_CheckStateChanged(sender, e);
            checkBox3_CheckStateChanged(sender, e);
        }


        // Use proxy
        private void checkBox2_CheckStateChanged(object sender, EventArgs e)
        {
            checkBoxUseDefaultProxy.Enabled = checkBoxUseProxy.Checked;
        }

        // Use default proxy
        private void checkBox3_CheckStateChanged(object sender, EventArgs e)
        {
            label1.Enabled = textBox2.Enabled = textBox1.Enabled = !checkBoxUseDefaultProxy.Checked;
        }

        // Cancel
        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        // Ulozit
        private void button1_Click(object sender, EventArgs e)
        {
            if (!validate())
            {
                const string message = "Nějaká hodnota na formuláři má špatný formát!";
                const string caption = "Chyba validace nastavení";
                const MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, message, caption, buttons, MessageBoxIcon.Error);

                return;
            }

            SettingsManager manager = SettingsManager.Instance;

            manager.AutomaticUpdate = checkBoxAutomaticVersionCheck.Checked;
            manager.ViewResults = checkBoxViewResults.Checked;
            manager.UseProxy = checkBoxUseProxy.Checked;
            if (checkBoxUseProxy.Checked)
            {
                manager.UseDefaultProxy = checkBoxUseDefaultProxy.Checked;
            }

            if (checkBoxUseProxy.Checked && !checkBoxUseDefaultProxy.Checked)
            {
                manager.ProxyIPAddress = IPAddress.Parse(textBox1.Text);
                manager.ProxyPort = Int32.Parse(textBox2.Text);
            }

            manager.DontChangeGenres = checkBoxDontChangeGenres.Checked;
            manager.DontChangeNames = checkBoxDontChangeNames.Checked;
            manager.LoadPicturesOnline = checkBoxLoadPicturesOnLine.Checked;
            manager.BetaInform = checkBoxBeta.Checked;            

            manager.saveToRegister();

            Close();
        }

        /// <summary>
        /// Validace policek
        /// </summary>
        /// <returns></returns>
        private bool validate()
        {
            if (checkBoxUseProxy.Checked && !checkBoxUseDefaultProxy.Checked)
            {
                if (String.IsNullOrEmpty(textBox1.Text) || String.IsNullOrEmpty(textBox2.Text)) return false;

                IPAddress pom;
                if (!IPAddress.TryParse(textBox1.Text, out pom)) return false;

                Int32 pom2;
                if (!Int32.TryParse(textBox2.Text, out pom2)) return false;
            }
            return true;
        }

        private void checkBoxAutomaticVersionCheck_CheckedChanged(object sender, EventArgs e)
        {
            checkBoxBeta.Enabled = checkBoxAutomaticVersionCheck.Checked;
            if (!checkBoxAutomaticVersionCheck.Checked)
            {
                checkBoxBeta.Checked = false;
            }
        }
    }
}
